package com.cx.common.http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

/**
 *
 * @Author:cxy
 * @Date:2019/10/24 15:11
 */
public class SimpleClient {
	public static void main(String[] args) throws IOException {
		HttpClient client = new HttpClient();
		// 设置代理服务器地址和端口

		// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
		// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成 https
		// HttpMethod method = new GetMethod("http://www.baidu.com");
		// 使用POST方法
		PostMethod method = new PostMethod("http://localhost:1050/shutdown");
		// HttpMethodParams params = method.getParams();
		// params.s
		method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		// method.setRequestBody(body);
		// HttpMethodParams params = method.getParams();
		client.executeMethod(method);
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印返回的信息
		System.out.println(method.getResponseBodyAsString());
		// 释放连接
		method.releaseConnection();
	}

	// private NameValuePair[] buildNameValuePairs(Map<String, String> params) {
	// Object[] keys = params.keySet().toArray();
	// NameValuePair[] pairs = new NameValuePair[keys.length];
	//
	// for (int i = 0; i < keys.length; i++) {
	// String key = (String) keys[i];
	// pairs[i] = new NameValuePair(key, params.get(key));
	// }
	//
	// return pairs;
	// }
}
